package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

import com.gusha.service.InfoQuestionService;
import com.gusha.dao.InfoQuestionDao;
import com.gusha.dao.UserDao;
import com.gusha.model.InfoQuestion;
import com.gusha.model.User;


/**
 * @Created：2018-11-08 10:07:03
 * @Author LightSound的代码生成器
 * @Description:InfoQuestionController控制类
 */
@Controller
@RequestMapping("/gx/infoQuestion")
public class InfoQuestionController {

    // 注入Dao
    @Autowired
    private InfoQuestionDao infoQuestionDao;
    @Autowired
    private UserDao userDao;
    
    // 注入Service
    @Autowired
    private InfoQuestionService infoQuestionService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看问答列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String userId = WebValueUtil.getWebValue(request, "userId");
        
        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        InfoQuestion infoQuestion = new InfoQuestion();
        if (StringUtils.isNotBlank(userId)) {
        	infoQuestion.setUser(userDao.getOne(new BigInteger(userId)));
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<InfoQuestion> page = infoQuestionService.findAll(pageable, infoQuestion);
        
        List<User> listUser = userDao.findAll();

        model.addAttribute("page", page);
        model.addAttribute("userId", userId);
        model.addAttribute("listUser", listUser);
        return "/admin/infoQuestion/list";
    }

    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除XX信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (infoQuestionDao.existsById(id)) {
            infoQuestionDao.deleteById(id);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }

}
